package com.hex.liyue.usermodule.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hex.liyue.R;
import com.hex.liyue.commonmodule.fragment.BaseFragment;
import com.hex.liyue.commonmodule.glide.GlideRoundTransform;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.giftmodule.activity.DiscountGiftsActivity;
import com.hex.liyue.giftmodule.activity.GiveGiftsActivity;
import com.hex.liyue.giftmodule.activity.HarvestGiftsActivity;
import com.hex.liyue.giftmodule.activity.MyGiftsActivity;
import com.hex.liyue.usermodule.activity.AddressActivity;
import com.hex.liyue.usermodule.activity.FriendsActivity;
import com.hex.liyue.usermodule.activity.MyOrderActivity;
import com.hex.liyue.usermodule.activity.PersonEditActivity;
import com.hex.liyue.usermodule.activity.SettingActivity;
import com.hex.liyue.usermodule.activity.WalletActivity;
import com.hex.liyue.usermodule.bean.MyMessageResultBean;
import com.hex.liyue.usermodule.presenter.IMeView;
import com.hex.liyue.usermodule.presenter.MePresenter;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 这是一个主界面的个人中心碎片
 * Created by WJ on 2016/6/6.
 */
public class MeFragment extends BaseFragment implements IMeView, View.OnClickListener {

    @Bind(R.id.iv_me_avater)
    ImageView ivMeAvator;
    @Bind(R.id.tv_toobar_right)
    TextView tvToobarRight;
    @Bind(R.id.tv_company)
    TextView tvCompany;
    private Intent intent;
    private MePresenter mePresenter;
    public static String mFlag;

    @Override
    public void initdata(Bundle arguments) {
        layoutLoading();
        mePresenter.getMyMessage();

        String user_type = UserManager.getUserType();
        L.e("user_type",user_type);
        if (user_type.equals("1")) {//普通用户  you 50    40
            if (TextUtils.isEmpty(UserManager.getInviteCode())) {
                tvCompany.setText("申请公司");
            }
        } else{
            tvCompany.setText("我的公司");
        }
    }

    @Override
    public void setInfo(MyMessageResultBean myMessageResultBean) {
        layoutContent();
        UserManager.showAll();
        UserManager.saveUserInfo(myMessageResultBean);
        UserManager.showAll();
        Glide.with(this)
                .load(UserManager.getUserAvator())
                .error(R.drawable.default_error)
                .transform(new GlideRoundTransform(getActivity(),4))
                .centerCrop()
                .into(ivMeAvator);
    }

    public void updateAvater() {
        Glide.with(this)
                .load(UserManager.getUserAvator())
                .error(R.drawable.default_error)
                .transform(new GlideRoundTransform(getActivity(),4))
                .centerCrop()
                .into(ivMeAvator);
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_me;
    }

    @Override
    protected void bindPresenter() {
        mePresenter = new MePresenter(this);
    }

    @Override
    protected void initToolbar() {
        tvToobarRight.setVisibility(View.VISIBLE);
        tvToobarRight.setText("设置");
    }

    @Override
    @OnClick({R.id.tv_toobar_right, R.id.ll_me_gift, R.id.ll_me_discount_gift, R.id.ll_me_harvest_gift, R.id.ll_me_give_gift,
            R.id.ll_me_wallet, R.id.ll_me_order, R.id.ll_me_address, R.id.ll_me_friends, R.id.rl_me_person})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_toobar_right:
                //设置
                startSettingActivity();
                break;
            case R.id.ll_me_gift:
                //我的礼物
                startMyGiftsActivity();
                break;
            case R.id.ll_me_discount_gift:
                //折现的礼物
                startDiscountGiftsActivity();
                break;
            case R.id.ll_me_harvest_gift:
                //收货的礼物
                startHarvetGiftsActivity();
                break;
            case R.id.ll_me_give_gift:
                //赠送的礼物
                startGiveGiftsActivity();
                break;
            case R.id.ll_me_wallet:
                //我的钱包
                startWalletActivity();
                break;
            case R.id.ll_me_order:
                //我的订单
                startOrderActivity();
                break;
            case R.id.ll_me_address:
                //我的地址
                startAddressActivity();
                break;
            case R.id.ll_me_friends:
                //我的伙伴
                startFriendsActivity();
                break;
            case R.id.rl_me_person:
                //我的资料
                startPersonInfoActivity();
                break;


        }
    }

    @Override
    public void showLod(String msg) {
        showLoad(getActivity(), msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(getActivity(), msg);
    }

    @Override
    public void startSettingActivity() {
        intent = new Intent(getActivity(), SettingActivity.class);
        getActivity().startActivity(intent);
    }

    @Override
    public void startAddressActivity() {
        intent = new Intent(getActivity(), AddressActivity.class);
        getActivity().startActivity(intent);
    }

    @Override
    public void startFriendsActivity() {
        intent = new Intent(getActivity(), FriendsActivity.class);
        getActivity().startActivity(intent);
    }

    @Override
    public void startWalletActivity() {
        intent = new Intent(getActivity(), WalletActivity.class);
        getActivity().startActivity(intent);
    }

    @Override
    public void startOrderActivity() {
        intent = new Intent(getActivity(), MyOrderActivity.class);
        getActivity().startActivity(intent);
    }

    @Override
    public void startPersonInfoActivity() {
        intent = new Intent(getActivity(), PersonEditActivity.class);
        getActivity().startActivityForResult(intent, 0);
    }

    @Override
    public void startMyGiftsActivity() {
        intent = new Intent(getActivity(), MyGiftsActivity.class);
        getActivity().startActivity(intent);
    }

    @Override
    public void startDiscountGiftsActivity() {
        intent = new Intent(getActivity(), DiscountGiftsActivity.class);
        getActivity().startActivity(intent);
    }

    @Override
    public void startHarvetGiftsActivity() {
        intent = new Intent(getActivity(), HarvestGiftsActivity.class);
        getActivity().startActivity(intent);
    }

    @Override
    public void startGiveGiftsActivity() {
        intent = new Intent(getActivity(), GiveGiftsActivity.class);
        getActivity().startActivity(intent);
    }

    @Override
    public void onResume() {
        super.onResume();
        if("success".equals(mFlag)){
            tvCompany.setText("我的公司");
        }
    }
}
